package com.sci99.ex.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.sci99.ex.model.Question;
import com.sci99.ex.util.MyBatisUtil;

public class QuestionService {
	static Logger log = Logger.getLogger(QuestionService.class);

	public static void addQuestion(List<Question> list) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		s.insert("addQuestionImport", list);
		s.commit();
		s.close();
	}

	public static List<Question> getQuestionByCourseId(Long cid)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> m = new HashMap<>(2, 1);
		m.put("courseId", cid);
		List<Question> l = s.selectList("getQuestion", m);
		s.close();
		return l;
	}

	public static void saveQuestion(Question q) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		if (q.getId() == null)
			s.insert("addQuestion", q);
		else
			s.update("uptQuestion", q);
		s.commit();
		s.close();
	}

	public static void delQuetsion(Question q) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		s.delete("delQuestion", q);
		s.commit();
	}

}
